package com.lxu.finance.service;

import com.lxu.finance.entity.YwInveSurveyReportIndex;
import com.lxu.finance.mapper.FinanceMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FinanceService {

    @Resource
    private FinanceMapper financeMapper;

    /**
     * 1、查询模板配置表，组装数据
     * 2、查询负债表，组装数据
     * 3、查询利润表，组装数据
     * 4、根据模板配置的信息，提取计算公式，将负债表和利润表的值填充，数据计算；
     * 5、填充财务指标分析表
     * @param templateType
     */
    public  List<Map<String,Object>>  findTemplate(String templateType){

        List<Map<String,Object>> templates = financeMapper.findTemplate(templateType);
        return  templates;
    }

    public  Map<String,String> findFinanceBlance(String inveSurveyDetailId){
        List<Map<String,Object>> blances = financeMapper.findFinanceBlance(inveSurveyDetailId);
        Map<String,String> map = new HashMap<>();
        if(CollectionUtils.isNotEmpty(blances)){
            String tableName = "yw_inve_survey_report_balance";
            blances.forEach(blance ->{
                map.put(assembleKey(tableName,String.valueOf(blance.get("subject_code")),"fund_one"),String.valueOf(blance.get("fund_one")));
                map.put(assembleKey(tableName,String.valueOf(blance.get("subject_code")),"fund_two"),String.valueOf(blance.get("fund_two")));
                map.put(assembleKey(tableName,String.valueOf(blance.get("subject_code")),"fund_three"),String.valueOf(blance.get("fund_three")));
                map.put(assembleKey(tableName,String.valueOf(blance.get("subject_code")),"fund_four"),String.valueOf(blance.get("fund_four")));
                map.put(assembleKey(tableName,String.valueOf(blance.get("subject2_code")),"debt_one"),String.valueOf(blance.get("debt_one")));
                map.put(assembleKey(tableName,String.valueOf(blance.get("subject2_code")),"debt_two"),String.valueOf(blance.get("debt_two")));
                map.put(assembleKey(tableName,String.valueOf(blance.get("subject2_code")),"debt_three"),String.valueOf(blance.get("debt_three")));
                map.put(assembleKey(tableName,String.valueOf(blance.get("subject2_code")),"debt_four"),String.valueOf(blance.get("debt_four")));
            });
        }
        return map;
    }

    private String assembleKey(String tableName, String record, String fieldName) {
        StringBuffer sb = new StringBuffer();
        return sb.append(tableName).append("#").append(record).append("#").append(fieldName).toString();
    }

    public Map<String, String> findFinanceProfit(String inveSurveyDetailId) {
        List<Map<String,Object>> profits = financeMapper.findFinanceProfit(inveSurveyDetailId);
        Map<String,String> map = new HashMap<>();
        if(CollectionUtils.isNotEmpty(profits)){
            String tableName = "yw_inve_survey_report_profit";
            profits.forEach(profit ->{
                map.put(assembleKey(tableName,String.valueOf(profit.get("subject_code")),"profit_one"),String.valueOf(profit.get("profit_one")));
                map.put(assembleKey(tableName,String.valueOf(profit.get("subject_code")),"profit_two"),String.valueOf(profit.get("profit_two")));
                map.put(assembleKey(tableName,String.valueOf(profit.get("subject_code")),"profit_three"),String.valueOf(profit.get("profit_three")));
                map.put(assembleKey(tableName,String.valueOf(profit.get("subject_code")),"profit_four"),String.valueOf(profit.get("profit_four")));
            });
        }
        return map;
    }

    public void deleteByInveSurveyDetailId(String inveSurveyDetailId) {
        financeMapper.deleteByInveSurveyDetailId(inveSurveyDetailId);
    }

    public void insert(YwInveSurveyReportIndex index) {
        financeMapper.insert(index);
    }

}
